<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8">
    <meta name="keywords" content="ES6语法归纳, HTML,CSS,JavaScript,JQuery,React,Vue.js等">
    <meta name="description" content="别做一个Vue api调用师">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>ES6语法归纳 | zihao blog</title>
    <link rel="icon" type="image/png" href="/pride-blog/favicon.png">

    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/css/my.css">

    <script src="/pride-blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.3.0"><link rel="stylesheet" href="/pride-blog/css/prism-tomorrow.css" type="text/css"><!-- hexo-inject:begin --><!-- hexo-inject:end --></head>




<body>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/pride-blog/" class="waves-effect waves-light">
                    
                    <img src="/pride-blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">zihao blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/pride-blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/pride-blog/categories/CSS/" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/pride-blog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/pride-blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">zihao blog</div>
        <div class="logo-desc">
            
            别做一个Vue api调用师
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/pride-blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/pride-blog/categories/CSS/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/pride-blog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/pride-blog/medias/featureimages/17.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">ES6语法归纳</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/pride-blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/pride-blog/tags/ES6/">
                                <span class="chip bg-color">ES6</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/pride-blog/categories/ES6/" class="post-category">
                                ES6
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-03-08
                </div>
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h3 id="本文归纳总结了-ES6-语法"><a href="#本文归纳总结了-ES6-语法" class="headerlink" title="本文归纳总结了 ES6 语法"></a>本文归纳总结了 ES6 语法</h3><h4 id="var-let-const-声明变量"><a href="#var-let-const-声明变量" class="headerlink" title="var / let / const 声明变量"></a>var / let / const 声明变量</h4><pre><code>1. var声明变量存在变量提升,无论var是在哪里声明,都会被提升到当前作用域的最顶部;
2. let不存在变量声明提升问题,并且声明的变量只在当前作用域块有效;
3. const 声明的为常量,变量不可更改,不可重复声明,不可修改值;const声明对象是可以修改值,但是不允许修改绑定;

const my_object = &#123;&quot;key&quot;: &quot;value&quot;&#125;;
const my_object = &#123;&quot;newKey&quot;: &quot;value&quot;&#125;; // Typeerror
&lt;!-- 当常量声明为一个对象时,这个对象值是只读的,不能对其重写,但是其属性值是不被保护的,可以对值进行修改或添加新属性 --&gt;
my_object.key =  &quot;newValue&quot;;
my_object.newKey =  &quot;newValue2&quot;;</code></pre>
<h4 id="解构赋值"><a href="#解构赋值" class="headerlink" title="解构赋值"></a>解构赋值</h4><pre><code>var obj = &#123;
    name:&quot;小明&quot;,
    age:&quot;18&quot;,
    callback:function() &#123;
        console.log(obj.name);
    &#125;
&#125;;
const &#123;name,age,callback&#125; = obj;
console.log(name);
console.log(age);
console.log(callback);
</code></pre>
<h4 id="字符串新增的方法"><a href="#字符串新增的方法" class="headerlink" title="字符串新增的方法"></a>字符串新增的方法</h4><h5 id="1-includes"><a href="#1-includes" class="headerlink" title="1. includes()"></a>1. includes()</h5><p>返回布尔值,当找到对应的字符串时,返回 true,否则返回 false;和 indexOf()类似;</p>
<pre><code>var string1 = &quot;hello,stranger&quot;
console.log(string1.includes(&quot;h&quot;)); // true
console.log(string1.includes(&quot;x&quot;)); // false
</code></pre>
<h5 id="2-startWith"><a href="#2-startWith" class="headerlink" title="2. startWith()"></a>2. startWith()</h5><p>返回布尔值,当找到对应的字符串时,返回 true,否则返回 false;表示该字段是否在字符串头部;</p>
<pre><code>var string1 = &quot;hello,stranger&quot;;
console.log(string1.startWith(&quot;h&quot;)); // true
</code></pre>
<h5 id="3-endWith"><a href="#3-endWith" class="headerlink" title="3. endWith()"></a>3. endWith()</h5><p>返回布尔值,当找到对应的字符串时,返回 true,否则返回 false;表示该字段是否在字符串尾部;</p>
<pre><code>var string1 = &quot;hello,stranger&quot;;
console.log(string1.endWith(&quot;r&quot;)); // true
</code></pre>
<h5 id="4-同时上面三个方法都支持接收第二个参数表示位置"><a href="#4-同时上面三个方法都支持接收第二个参数表示位置" class="headerlink" title="4. 同时上面三个方法都支持接收第二个参数表示位置"></a>4. 同时上面三个方法都支持接收第二个参数表示位置</h5><pre><code>var string1 = &quot;hello,stranger&quot;;
console.log(string1.includes(&quot;stranger&quot;,6)); // true
console.log(string1.startWith(&quot;h&quot;,0)); // true
console.log(string1.endWith(&quot;h&quot;,13)); // true
</code></pre>
<h5 id="5-trimStart-和-trimEnd"><a href="#5-trimStart-和-trimEnd" class="headerlink" title="5. trimStart()和 trimEnd()"></a>5. trimStart()和 trimEnd()</h5><p>和 trim()方法是类似的;不过 trim 是去除前后的空格;<br>trimStart()是去除字符串前面的空格,保留尾部的空格;trimEnd()则是去除字符串尾部的空格,保留前面的空格;</p>
<pre><code>
var string1 = &quot; hello,stranger &quot;;
console.log(string1.trim()); // &quot;hello,stranger&quot;;
console.log(string1.trimStart()); // &quot;hello,stranger &quot;;
console.log(string1.trimEnd()); // &quot; hello,stranger&quot;;
</code></pre>
<h4 id="解构赋值-1"><a href="#解构赋值-1" class="headerlink" title="解构赋值"></a>解构赋值</h4><pre><code>var obj = &#123;
name:&quot;小明&quot;,
age:&quot;18&quot;,
callback:function() &#123;
console.log(obj.name);
&#125;
&#125;;
const &#123;name,age,callback&#125; = obj;
console.log(name);
console.log(age);
console.log(callback);</code></pre>
<h4 id="函数传参的默认值"><a href="#函数传参的默认值" class="headerlink" title="函数传参的默认值"></a>函数传参的默认值</h4><pre><code>
function request(url,timeout,callback) &#123;
timeout = timeout || 2000;
callback = callback || function()&#123;&#125;;
&#125;

ES6 新增:
function request(url,timeout = 2000,callback = function() &#123;&#125;) &#123;
timeout = timeout || 2000;
callback = callback || function()&#123;&#125;;
&#125;

&lt;!-- 可以直接在传参的时候进行赋值 --&gt;

request(&quot;foo&quot;,undefined,function()&#123;&#125;);
// timeout 为默认值

request(&quot;foo&quot;,null,function()&#123;&#125;);
// timeout 值为 null;
null 为合法的值;
</code></pre>
<h4 id="拓展运算符"><a href="#拓展运算符" class="headerlink" title="拓展运算符"></a>拓展运算符</h4><pre><code>
1. 可用来合并数组
   var a = [1,2,3];
   var b = [4,5,6];
   var c = [...a,...b];

2.浅拷贝
var a = &#123;a:1&#125;;
var b = &#123;...a&#125;;
a.a = 5;
console.log(b.a); // 1
</code></pre>
<h4 id="数组的拓展"><a href="#数组的拓展" class="headerlink" title="数组的拓展"></a>数组的拓展</h4><p><strong>1. Array.from()</strong></p>
<p>Array.from()可以将类数组转化为真正的数组;</p>
<pre><code>let arrayLike = &#123;
    &quot;0&quot;:&quot;a&quot;,
    &quot;1&quot;:&quot;b&quot;,
    &quot;2&quot;:&quot;c&quot;,
&#125;;
&lt;!-- ES5的写法 --&gt;
var arr1 = [].slice.call(arrayLike);
console.log(arr1); // [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]

&lt;!-- ES6的写法 --&gt;
var arr2 = Array.form(arrayLike);
console.log(arr2); // [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code></pre>
<p><strong>2. Array.of()</strong></p>
<p>Array.of()可以将一组数据转化为数组;</p>
<pre><code>var arr1 = Array.of(1,2,3,4);
console.log(arr1); // [1,2,3,4]
var arr2 - Array.of(undefined);
console.log(arr2); // [undefined]</code></pre>
<p><strong>3. find()和 findIndex()</strong></p>
<p>find()是找出数组中符合条件的第一个成员,并返回该成员,如果没有符合该条件的成员,则返回 undefined;</p>
<pre><code>var arr1 = [1,2,3,4,5,6,7];
var search = arr1.find((n) =&gt; &#123;
    return n &gt; 6;
&#125;);
console.log(search); // 7</code></pre>
<p>同时,find()回调函数接收三个参数,分别为 value,index,arr;位置,值,数组;</p>
<pre><code>var arr1 = [1,2,3,4,5,6,7];
var search = arr1.find((value,index,arr) =&gt; &#123;
    return value &gt; 6;
&#125;);
console.log(search); // 7</code></pre>
<p>findIndex()和 find()类似,返回第一个符合条件的数组的成员的位置,如果所有成员都不符合条件,则返回-1;</p>
<pre><code>var arr3 = [1,2,3,4,5,6,7];
var search = arr3.findIndex((value,index,arr) =&gt; &#123;
    return value &gt; 6;
&#125;);
console.log(search);</code></pre>
<p><strong>4. flat()和 flatMap()</strong></p>
<p>flat()用于将嵌套的数组拉平,将二维数组、三维数组扁平化;</p>
<pre><code>var arr1 = [1,2,3,[4,5,6,7]];
var arr2 =arr1.flat();
console.log(arr2); // [1,2,3,4,5,6,7]</code></pre>
<p>但是 flat()默认只能拉平一层,如果想要拉平多层,则需要传参;</p>
<pre><code>var arr1 = [1,2,3,[4,5,6,7,[8,9,10]]];
var arr2 =arr1.flat();
console.log(arr2); // [1,2,3,[4,5,6,7,8,9,10]]
var arr3 = arr1.flat(2);
console.log(arr3); // [1,2,3,4,5,6,7,8,9,10]
</code></pre>
<p>如果想要拉平很多层,可以直接传参 Infinity;</p>
<pre><code>var arr1 = [1,2,3,[4,5,6,7,[8,9,10]]];
var arr2 =arr1.flat(Infinity);
console.log(arr2); // [1,2,3,4,5,6,7,8,9,10]
</code></pre>
<p>flatMap()相当于先对数组执行 Array.map(),再对返回的数组执行 flat(),该方法返回一个新数组,不改变原数组;</p>
<pre><code>var arr1 = [1,2,3];
var arr2 =arr1.flatMap((x) =&gt; &#123;
    [x * 2]
&#125;);
&lt;!-- 相当于先执行map方法,返回新数组[[2],[4],[6]],再对新数组执行flat() --&gt;
console.log(arr2); // [2,4,6]
</code></pre>
<p><strong>5. sort()</strong></p>
<p>sort()用于排序,返回一个新数组;</p>
<pre><code>var arr = [1,5,2,10,9];
var newArr = arr.sort((a,b) =&gt; &#123;
    return a-b;
&#125;);
console.log(newArr); // [1,2,5,9,10]</code></pre>
<p><strong>6. set 和 map</strong></p>
<p>Set 是一个构造函数,为 ES6 提供的一个新的数据结构,类似于数组,但是该数据结构不能有重复的成员;所以可以用来数组的去重;</p>
<pre><code>var arr = [1,2,2,4,5,4,6];
var s = new Set(arr);
console.log(s); // [1,2,4,5,6]</code></pre>
<p>Set.prototype 上有四个操作数据的方法<br><strong>6.1 Set.prototype.add(value)</strong><br>添加某个值,返回 Set 结构本身;<br><strong>6.2 Set.prototype.delete(value)</strong><br>删除某个值,返回 Set 结构本身;<br><strong>6.3 Set.prototype.has(value)</strong><br>判断 value 是否为 Set 的成员;返回布尔值;存在返回 true,不存在返回 false;<br><strong>6.4 Set.prototype.clear()</strong><br>清除 Set 里面的成员,初始化;没有返回值;</p>
<p>Array.from()可以将 Set 数据结构转成真正的数据;</p>
<pre><code>const items = new Set([1, 2, 3, 4, 5]);
const array = Array.from(items);
console.log(array); // [1,2,3,4,5]
</code></pre>
<p><strong>6.1 WeakSet</strong></p>
<p>WeakSet 是一个构造函数,为 ES6 提供的一个新的数据结构,和 Set 类似,也是该数据结构不能有重复的成员;<br>但是 WeakSet 的成员只能是对象,不能是其他属性的值;<br>另外,WeakMap 是弱引用,即垃圾回收机制不考虑 WeakSet 对该对象的引用;<br>就是说如果其他对象不再引用该对象,那么垃圾回收机制就会自动回收该对象所占用的内存;<br>不考虑该对象还存在于 WeakSet 中;</p>
<pre><code>const ws = new WeakSet();
&lt;!-- WeakSet作为构造函数接收一个数组或者类似数组的对象作为参数,该数组所有的成员,都会自动成为WeakSet实例对象的成员 --&gt;
const arr = [[1,2],[3,4]];
const ws2 = new WeakSet(arr);
console.log(ws2); // WeakSet&#123;[1,2],[3,4]&#125;
</code></pre>
<p><strong>注意:arr 是一个数组,它有两个成员,也是数组,是 arr 的两个成员成为了 WeakSet 的成员,不是 arr 数组本身,这也意味着数组的成员只能是对象</strong></p>
<pre><code>const b = [1,2,3];
const ws3 = new WeakSet(b);
console.log(ws3); // 报错 :Uncaught TypeError；Invalid value used is weak set(...)</code></pre>
<p>WeakSet 结构有三个方法<br><strong>WeakSet.prototype.add(value)</strong><br>向 WeakSet 实例中添加某个 value;<br><strong>WeakSet.prototype.delete(value)</strong><br>向 WeakSet 实例中删除某个 value;<br><strong>WeakSet.prototype.has(value)</strong><br>判断 WeakSet 实例中是否有 value 该值,存在返回 true,否则返回 false;</p>
<p><strong>7 Map</strong></p>
<p>Object 提供的对象实例中的键值对,实际上只能使用字符串做键;<br>而 Map 提供的数据结构,它类似于对象,但是不同的是，它的键不仅仅局限于字符串,包括各种类型的值(包括对象),都可以成为它的键;<br>总结:Object 对象提供的是一种“字符串-值”的集合,而 Map 数据结构提供的“值-值”的集合;</p>
<pre><code>const m = new Map();
const obj = &#123;sayHi:&quot;Hello world&quot;&#125;;
m.set(obj,&quot;content&quot;);
m.get(obj); // &quot;content&quot;
</code></pre>
<pre><code>const map = new Map([
  [&#39;name&#39;, &#39;张三&#39;],
  [&#39;title&#39;, &#39;Author&#39;]
]);

map.size // 2
map.has(&#39;name&#39;) // true
map.get(&#39;name&#39;) // &quot;张三&quot;
map.has(&#39;title&#39;) // true
map.get(&#39;title&#39;) // &quot;Author&quot;
</code></pre>
<p>Map 结构有四个方法<br><strong>Map.prototype.add(value)</strong><br>向 Map 实例中添加某个 value;<br><strong>Map.prototype.delete(value)</strong><br>向 Map 实例中删除某个 value;<br><strong>Map.prototype.has(value)</strong><br>判断 Map 实例中是否有 value 该值,存在返回 true,否则返回 false;<br><strong>6.4 Set.prototype.clear()</strong><br>清除 Map 里面的成员,初始化;没有返回值;</p>
<p><strong><font color=#FF0000 >另外值得注意的是:只有对同一个对象的引用,Map 结构才将其视为同一个键;</font></strong></p>
<pre><code>const map = new Map();
map.set([&#39;a],aaa);
map.get([&#39;a]); // undefined</code></pre>
<p><strong><font color=#FF0000 >上述代码中表面上针对的是同一个键,但是实际上是不同的两个数组,内存地址是不同的</font></strong></p>
<pre><code>const map = new Map();
const k1 = [&#39;a&#39;];
const k2 = [&#39;a&#39;];
map.set(k1,111).set(k2,222);
map.get(k1); // 111;
map.get(k2); // 222;</code></pre>
<p><strong>Map 和其他数据结构进行转化</strong></p>
<ol>
<li>Map 转化为数组;</li>
</ol>
<pre><code>const myMap = new Map()
  .set(true, 7)
  .set(&#123;foo: 3&#125;, [&#39;abc&#39;]);
[...myMap]
// [ [ true, 7 ], [ &#123; foo: 3 &#125;, [ &#39;abc&#39; ] ] ]</code></pre>
<ol start="2">
<li>数组转化为 Map;</li>
</ol>
<pre><code>new Map([
  [true, 7],
  [&#123;foo: 3&#125;, [&#39;abc&#39;]]
])
// Map &#123;
//   true =&gt; 7,
//   Object &#123;foo: 3&#125; =&gt; [&#39;abc&#39;]
// &#125;</code></pre>
<ol start="3">
<li><p>Map 转化为对象;</p>
<p><img src="https://i.loli.net/2021/03/09/TtdkeQYbpDj489V.png" alt="Map 转化为对象"></p>
</li>
</ol>
<pre><code>function strMapToObj(strMap) &#123;
  let obj = Object.create(null);
  for (let [k,v] of strMap) &#123;
    obj[k] = v;
  &#125;
  return obj;
&#125;

const myMap = new Map()
  .set(&#39;yes&#39;, true)
  .set(&#39;no&#39;, false);
strMapToObj(myMap)
// &#123; yes: true, no: false &#125;
</code></pre>
<ol start="4">
<li>对象转化为 Map;</li>
</ol>
<pre><code>let obj = &#123;&quot;a&quot;:1, &quot;b&quot;:2&#125;;
let map = new Map(Object.entries(obj));
</code></pre>
<p><strong>7.1 WeakMap</strong><br>WeakMap 和 Map 类似,也是生成键值对的集合;区别两点;</p>
<ol>
<li>WeakMap 只接收对象作为键名(null 除外);不接收其他类型的值做键名;</li>
<li>WeakMap 的键名所指向的对象,不计入垃圾回收机制;<br>它的键名所引用的对象是弱引用;当外部没有引用该对象,那么垃圾回收机制就会自动回收该对象所占用的内存;<br>不考虑该对象还存在于 WeakSet 中;</li>
</ol>

                
            </div>
            <hr/>

            



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/pride-blog/tags/ES6/">
                                    <span class="chip bg-color">ES6</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/share/css/share.min.css">
<div id="article-share">

    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/pride-blog/2021/030928998.html">
                    <div class="card-image">
                        
                        
                        <img src="/pride-blog/medias/featureimages/14.jpg" class="responsive-img" alt="ES6-Promise对象">
                        
                        <span class="card-title">ES6-Promise对象</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-03-09
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/pride-blog/categories/ES6/" class="post-category">
                                    ES6
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/pride-blog/tags/ES6/">
                        <span class="chip bg-color">ES6</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/pride-blog/2021/030826937.html">
                    <div class="card-image">
                        
                        
                        <img src="/pride-blog/medias/featureimages/23.jpg" class="responsive-img" alt="CSS-rem和em的区别">
                        
                        <span class="card-title">CSS-rem和em的区别</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-03-08
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/pride-blog/categories/CSS/" class="post-category">
                                    CSS
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/pride-blog/tags/CSS/">
                        <span class="chip bg-color">CSS</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/pride-blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <span id="year">2021</span>
            <a href="/pride-blog/about" target="_blank">zihao xiong</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link ">














</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/pride-blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/pride-blog/libs/materialize/materialize.min.js"></script>
    <script src="/pride-blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/pride-blog/libs/aos/aos.js"></script>
    <script src="/pride-blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/pride-blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/pride-blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/pride-blog/libs/others/clicklove.js" async="async"></script>
    
    

    

    

    <!--腾讯兔小巢-->
    
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/pride-blog/libs/background/canvas-nest.js"></script>
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="/pride-blog/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="/pride-blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="/pride-blog/libs/instantpage/instantpage.js" type="module"></script><!-- hexo-inject:begin --><!-- hexo-inject:end -->
    

</body>
<script src="./../source/js/cursor.js"></script>

</html>
